package com.service;
import com.alibaba.fastjson.JSON;
import com.dao.AsRoleDao;
import com.entity.AsRole;
import com.entity.AsUser;
import com.utils.JsonUtil;
import com.utils.Pager;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;

@Service
public class AsRoleService
{
    @Resource
    private AsRoleDao asRoleDao;

    //返回全部可用角色
    public ArrayList<AsRole> returnAllAvailableRole()
    {
        return asRoleDao.getAllAvailableRole();
    }

    //返回全部可用角色json数据
    public String returnAllAvailableRoleJsonStr()
    {
        ArrayList<AsRole> roleList = asRoleDao.getAllAvailableRole();
        return JsonUtil.toJsonStr( roleList );
    }

    //返回全部角色数量
    public Integer returnAllRoleCount()
    {
        return asRoleDao.getAllRoleCount();
    }

    //返回角色管理页面json格式数据
    public String returnRoleManageDataGridJsonStr( Integer page , Integer size )
    {
        Pager<AsRole> pager = new Pager<>();
        //获取角色数量
        Integer total = asRoleDao.getAllRoleCount();
        pager.setTotal( total );
        //System.out.println( "total: " + total );
        Integer index = ( page - 1 ) * size;
        ArrayList<AsRole> roleList = asRoleDao.getSomeRole( index , size );
        pager.setRows( roleList );
        String jsonStr = JsonUtil.toJsonStrWithDateTime( pager );

        return jsonStr;
    }

    //通过id返回1个角色
    public String returnOneRoleById( Integer id )
    {
        AsRole role =  asRoleDao.getOneRoleById( id );
        String jsonStr = JsonUtil.toJsonStrWithDateTime( role );
        return jsonStr;
    }

    //更新一个角色
    public String updateRole( AsRole role )
    {
        role.setLastUpdateTime( new Date() );
        Integer resultCode = asRoleDao.updateOneRole( role );
        String jsonStr = JsonUtil.successOrNotJsonStr( resultCode );
        return jsonStr;
    }

    //添加一个角色
    public String addRole( AsRole role , AsUser user )
    {
        role.setCreationTime( new Date() );
        role.setCreatedBy( user.getUserName() );
        role.setLastUpdateTime( new Date() );
        Integer resultCode = asRoleDao.addOneRole( role );
        String jsonStr = JsonUtil.successOrNotJsonStr( resultCode );
        return jsonStr;
    }

    public String delRole( Integer id )
    {
        Integer resultCode = asRoleDao.delOneRoleById( id );
        String jsonStr = JsonUtil.successOrNotJsonStr( resultCode );
        return jsonStr;
    }
}
